% show a line that grows and shrinks with f and j keys
global ROOT_STRUCT
rInit('local');
rAdd('dXline', 1, 'x1', -15, 'y1', 0 , 'x2', -15, 'y2', 0, ...
    'visible', true, 'penWidth', 50, 'color', [255 255 0]);

%%%sets up parameters for first random line + number
f= normrnd(100, 40)
g=round(f)
h=.1*g-15
%%%Top and bottom white lines
top = rAdd('dXline', 1, 'x1', -15, 'y1', 2 , 'x2', 15, 'y2', 2, ...
    'visible', true, 'penWidth', 20, 'color', [255 255 255]);
botton = rAdd('dXline', 1, 'x1', -15, 'y1', -1 , 'x2', 15, 'y2', -1, ...
    'visible', true, 'penWidth', 10, 'color', [255 255 255]);

%%random line and random number (invisible until h pushed)
random = rAdd('dXline', 1, 'x1', -15, 'y1', 1 , 'x2', h, 'y2', 1, ...
    'visible', false, 'penWidth', 5, 'color', [0 0 255]);
rand = rAdd('dXtext', 1, 'size', 60, 'string', g, 'x', 0, 'y', 5 , ...
    'visible', false, 'color', [0 0 200]);


%%sets subject guess at zero and makes line 0 length
v=0

guess = rAdd('dXtext', 1, 'size', 60, 'string', v, 'x', 0, 'y', -3 , ...
    'visible', true, 'color', [255 255 0]);


%%adds line and text describing the difference between guess and rand

k=abs(v-g)

diffText = rAdd('dXtext', 1, 'size', 40, 'string', k, 'x', -6, 'y', 13 , ...
    'visible', false, 'color', [0 255 255]);



diff = rAdd('dXline', 1, 'x1', g, 'y1', .5 , 'x2', (.1*v)-15, 'y2', .5, ...
    'visible', false, 'penWidth', 5, 'color', [0 255 255]);




%% add score and total score text and scores



score = rAdd('dXtext', 1, 'size', 40, 'string', 'last score', 'x', -15, 'y', 13 , ...
    'visible', true, 'color', [0 255 255]);

totScore = rAdd('dXtext', 1, 'size', 40, 'string', 'total score', 'x', 4, 'y', 13 , ...
    'visible', true, 'color', [255 0 0]);


z=0
totDiff = rAdd('dXtext', 1, 'size', 40, 'string', z, 'x', 12, 'y', 13 , ...
    'visible', true, 'color', [255 0 0]);


rGraphicsDraw;










% how fast does the bar change?
step = .1;

f = KbName('f');
j = KbName('j');
q = KbName('q');

ROOT_STRUCT.dXkbHID = reset(ROOT_STRUCT.dXkbHID);
rGraphicsDraw;

ii = 1;
t = zeros(1,1e5);

while true

    HIDx('run');
    v = get(ROOT_STRUCT.dXkbHID, 'values');
    if ~isempty(v)
        if any(v(:,1) == q & v(:,2) == 1)

            % quitting takes priority
            break

        elseif any((v(:,1) == j | v(:,1) == f) & v(:,2) == 0)

            % detect button releases ahead of button presses
            ROOT_STRUCT.dXkbHID = reset(ROOT_STRUCT.dXkbHID);

        elseif any(v(:,1) == f & v(:,2) == 1)

            % detect left press
            rSet('dXline', 1, 'x2', rGet('dXline', 1, 'x2')-step);
            v=v-1;
            rSet('dXtext', guess, 'size', 60, 'string', v, 'x', 0, 'y', -3 , ...
                'visible', true, 'color', [255 255 0]);
            rGraphicsDraw;

        elseif any(v(:,1) == j & v(:,2) == 1)

            % detect right press
            rSet('dXline', 1, 'x2', rGet('dXline', 1, 'x2')+step);
            v=v+1;
            rSet('dXtext',guess, 'size', 60, 'string', v, 'x', 0, 'y', -3 , ...
                'visible', true, 'color', [255 255 0]);
            rGraphicsDraw;

        end
    end
    WaitSecs(.002);
end
rDone